package com.mcptt.db.model;

import java.io.Serializable;
import java.util.List;

import com.cloudptt.api.comm.CheckData;
import com.mcptt.db.DbConst;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.DatabaseUtils;

public class GroupInfo extends AddrInfo implements Serializable
{   
	public static final String Group_Num_All = "Group_All";
	public static final String Group_Name_All = "Group_All";

	private String groupId = null;
	private List<UserInfo> memberList = null;
	
	public GroupInfo(int type,String name,String addrId,int attr)
	{
		super(type,name,addrId,attr);
	}

	public GroupInfo(String addrId, String name, String groupId) {
		super(GroupType_Normal,name,addrId,DEFAULT_ATTR);
		this.groupId = groupId;
	}

	public GroupInfo(Cursor c)
	{
		super();
		ContentValues args = new ContentValues();
		DatabaseUtils.cursorRowToContentValues(c, args);
		createFromContentValue(args);
	}
	
	public static GroupInfo getDefaultUserInfo()
	{
		GroupInfo info = new GroupInfo(AddrInfo.GroupType_Normal,Group_Num_All,Group_Name_All,0);
		return info;
	}

	public boolean isSameOne(AddrInfo oldInfo)
	{
		if(oldInfo == null)
		{
			return false;
		}
		String oldMcId = oldInfo.addrId;
		if(this.addrId != null
				&& this.addrId.equals(oldMcId))
		{
			return true;
		}
		return false;
	}

	public boolean isSameOne(String oldNum)
	{
		if(this.addrId.equals(oldNum))
		{
			return true;
		}
		return false;
	}

	/**
	 * Pack the object content value to store
	 * 
	 * @return The content value representing the message
	 */
	public ContentValues getContentValues() {
		ContentValues cv = new ContentValues();
		cv.put(DbConst.COL_AddrId, addrId);
		cv.put(DbConst.COL_Type,type);
		cv.put(DbConst.COL_Name, name);
		cv.put(DbConst.COL_Attr, attr);
		cv.put(DbConst.COL_GroupId, groupId);
		return cv;
	}
	public ContentValues getSetDefaultGroupInfoContentValues() {
		ContentValues cv = new ContentValues();
		cv.put(DbConst.COL_AddrId, addrId);
//		cv.put(DbConst.COL_Type,type);
//		cv.put(DbConst.COL_Name, name);
		cv.put(DbConst.COL_Attr, attr);

		return cv;
	}
	public static ContentValues getUpdateContentValues(String name) 
	{
		ContentValues cv = new ContentValues();
		cv.put(DbConst.COL_Name, name);
		return cv;
	}
	
	public static ContentValues getDefaultGroupInfoContentValues() 
	{
		ContentValues cv = new ContentValues();
		cv.put(DbConst.COL_Type, GroupType_Normal);
		return cv;
	}
	
	public static ContentValues getClearDefaultGroupInfoContentValues() 
	{
		ContentValues cv = new ContentValues();
		cv.put(DbConst.COL_Type, GroupType_Normal);
		return cv;
	}
//	public static ContentValues getUpdateContentValues(int userCount) 
//	{
//		ContentValues cv = new ContentValues();
//		cv.put(DbConst.COL_Count, userCount);
//		return cv;
//	}

	public void createFromContentValue(ContentValues args) {

//		Log.debug(this.getClass(), "createFromContentValue");
		String tmp_s;
		tmp_s = args.getAsString(DbConst.COL_AddrId);
		if(tmp_s != null) {
			addrId = tmp_s;
		}
		tmp_s = args.getAsString(DbConst.COL_Type);
		if(tmp_s != null) {
			type = CheckData.parseInt(tmp_s);
		}
		tmp_s = args.getAsString(DbConst.COL_Name);
		if(tmp_s != null) {
			name = tmp_s;
		}
		tmp_s = args.getAsString(DbConst.COL_Attr);
		if(tmp_s != null) {
			int value = CheckData.parseInt(tmp_s);
			if(value != -1)
			{
				attr = value;
			}
		}
		tmp_s = args.getAsString(DbConst.COL_GroupId);
		if(tmp_s != null) {
			groupId = tmp_s;
		}
	}


	public boolean isAffiliation()
	{
		if(attr ==1)
		{
			return true;
		}
		return false;
	}

	public void setIsAffiliation(boolean isAffiliatio)
	{
		if(isAffiliatio)
		{
			attr =1;
		}
		else
		{
			attr = 0;
		}
	}
	public String getGroupId() {
		return groupId;
	}
	public List<UserInfo> getMemberList() {
		return memberList;
	}

	public void setMemberList(List<UserInfo> userInfo) {
		this.memberList = userInfo;
	}

	public String toString()
	{
		StringBuffer strBuf = new StringBuffer();
		strBuf.append("userType = ").append(type).append(",");
		strBuf.append("mcId = ").append(addrId).append(",");
		strBuf.append("name = ").append(name).append(",");
		strBuf.append("attr = ").append(attr).append(",");
		strBuf.append("groupId = ").append(groupId).append(",");
		strBuf.append("memberList = ").append(memberList).append(".");

		return strBuf.toString();
	}
}
